﻿using DapperExtensions;
using KD.Service.HR.Utility;
using Logistics.Model;
using Logistics.Service;
using Logistics.WebApi.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using WMLogistics.Model;
using WMLogistics.Service;

namespace Logistics.WebApi.Controllers
{
    [MyAuthority]
    public class WMReportBaseController : ApiController
    {
        private WMReportBidService service = new WMReportBidService();
        private CompanyService companyService = new CompanyService();
        /// <summary>
        /// 查询标单帐单
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        public ResponseModel GetPageList(ReportQuery query)
        {

            ResponseModel resultmod = new ResponseModel();
            var accountId = Convert.ToInt32(HttpContext.Current.Request.Headers[LogisticsConst.LOGIN_HEADER]);
            int company_id = companyService.GetCompanyIdByAccountId(accountId);

            try
            {
                var list = new List<IPredicate>();
                if (query.bid_no.IfNotNull())
                {
                    list.Add(Predicates.Field<wuliu_wm_report_base>(f => f.bid_no, Operator.Like,"%" +query.bid_no+"%"));
                }
                //if (query.consignee_time1 > 0)
                //    list.Add(Predicates.Field<wuliu_wm_report_base>(f => f.pickup_time, Operator.Ge, query.consignee_time1 / 1000));
                //if (query.consignee_time2 > 0)
                //    list.Add(Predicates.Field<wuliu_wm_report_base>(f => f.pickup_time, Operator.Le, query.consignee_time2 / 1000));


                if (!string.IsNullOrEmpty(query.orderNumber))
                    list.Add(Predicates.Field<wuliu_wm_report_base>(f => f.order_num, Operator.Like, "%"+query.orderNumber+"%"));
                list.Add(Predicates.Field<wuliu_wm_report_base>(f => f.company_id, Operator.Eq, company_id));
                if (query.wuliuname.IfNotNull())
                {
                    list.Add(Predicates.Field<wuliu_wm_report_base>(f => f.wuliu_name, Operator.Like, "'%" + query.wuliuname + "%'"));
                }

                if (query.state.IfNotNull())
                {
                    list.Add(Predicates.Field<wuliu_wm_report_base>(f => f.state, Operator.Eq, query.state));
                }
                var predicate = Predicates.Group(GroupOperator.And, list.ToArray());
                resultmod.InitResultState(GetDataStateEnum.Success);
                resultmod.mod = service.GetReportList(predicate, query.page - 1, query.pagesize);
            }
            catch(Exception ex)
            {
                resultmod.InitResultState(ex);
            }
           
            return resultmod;
        }
        /// <summary>
        /// 把未结变成已结
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        public ResponseModel OperationReport(ReportQuery query)
        {
            ResponseModel resultmod = new ResponseModel();
            string str = string.Empty;
            try
            {
                if (query.ids.IfNotNull())
                {
                    var list = query.ids.Split(',');
                    resultmod.InitResultState(GetDataStateEnum.Success);
                    resultmod.mod = service.OperatReportList(list);
                }
            }
            catch (Exception ex) { resultmod.InitResultState(ex); }

            return resultmod;
        }
    }
}